<?php

namespace app\it\controller;

use app\department\model\UserModel;
use app\rongcui\model\RongcuiBuyerModel;
use app\rongcui\model\RongcuiPotentialCustomerModel;
use cmf\controller\AdminBaseController;

class RongcuiController extends AdminBaseController
{
    public function buyer() {
        $time = [
            'starttime' => strtotime(date('Y-m-d')),
        ];
        $time['endtime'] = $time['starttime'] + 86399;
        if (input('start_time')) {
            $time['starttime'] = strtotime(input('start_time'));
        }
        if (input('end_time')) {
            $time['endtime'] = strtotime(input('end_time')) + 86399;
        }

        $gids = getGroups(5);
        $where = [
            'duser.dep_id' => ['in',$gids],
            'user_status' => ['neq',0]
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname'] = ['like', "%$keyword%"];
        }

        $rongcuiBuyerModel = new RongcuiBuyerModel();
        $data = $rongcuiBuyerModel
            ->alias('buyer')
            ->join('__RONGCUI_BUYER_USER__ buser','buyer.id = buser.pid and level = 1')
            ->field('buser.uid,count(buser.uid) as ccount')
            ->where([
                'buyer.create_time' => ['between', [$time['starttime'], $time['endtime']]]
            ])
            ->group('buser.uid')
            ->buildSql();

        $userModel = new UserModel();
        $lists = $userModel
            ->alias('user')
            ->join('__DEPARTMENT_USER__ duser','user.id = duser.user_id')
            ->join('__ROLE_USER__ role_user','role_user.user_id = user.id','left')
            ->join('__ROLE_ITEM__ role_item','role_user.role_id = role_item.role_id and config_id = 1','left')
            ->join('__DEPARTMENT_CONFIG_ITEM__ item','item.id = role_item.item_id and item.pid = 1','left')
            ->join('__DEPARTMENT__ dep','duser.dep_id = dep.id')
            ->join('__ROLE__ role','role_user.role_id = role.id')
            ->join($data . ' data','user.id = data.uid','left')
            ->where($where)
            ->field('user.user_nickname,ifnull(ccount,0) ccount,item.name iname,dep.name dname,role.name rname')
            ->order('data.ccount desc')
            ->select();

        $this->assign('lists',$lists);
        $this->assign('time', $time);
        return $this->fetch();
    }

    public function potential_customer() {
        $time = [
            'starttime' => strtotime(date('Y-m-d')),
        ];
        $time['endtime'] = $time['starttime'] + 86399;
        if (input('start_time')) {
            $time['starttime'] = strtotime(input('start_time'));
        }
        if (input('end_time')) {
            $time['endtime'] = strtotime(input('end_time')) + 86399;
        }

        $gids = getGroups(5);
        $where = [
            'duser.dep_id' => ['in',$gids],
            'user_status' => ['neq',0]
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname'] = ['like', "%$keyword%"];
        }

        $rongcuiPotentialCustomerModel = new RongcuiPotentialCustomerModel();
        $data = $rongcuiPotentialCustomerModel
            ->field('uid,count(uid) as ccount')
            ->where([
                'create_time' => ['between', [$time['starttime'], $time['endtime']]]
            ])
            ->group('uid')
            ->buildSql();

        $userModel = new UserModel();
        $lists = $userModel
            ->alias('user')
            ->join('__DEPARTMENT_USER__ duser','user.id = duser.user_id')
            ->join('__ROLE_USER__ role_user','role_user.user_id = user.id','left')
            ->join('__ROLE_ITEM__ role_item','role_user.role_id = role_item.role_id and config_id = 1','left')
            ->join('__DEPARTMENT_CONFIG_ITEM__ item','item.id = role_item.item_id and item.pid = 1','left')
            ->join('__DEPARTMENT__ dep','duser.dep_id = dep.id')
            ->join('__ROLE__ role','role_user.role_id = role.id')
            ->join($data . ' data','user.id = data.uid','left')
            ->where($where)
            ->field('user.user_nickname,ifnull(ccount,0) ccount,item.name iname,dep.name dname,role.name rname')
            ->order('data.ccount desc')
            ->select();

        $this->assign('lists',$lists);
        $this->assign('time', $time);
        return $this->fetch();
    }

    public function data() {

        $time = [
            'starttime' => strtotime(date('Y-m-d')),
        ];
        $time['endtime'] = $time['starttime'] + 86399;
        if (input('start_time')) {
            $time['starttime'] = strtotime(input('start_time'));
        }
        if (input('end_time')) {
            $time['endtime'] = strtotime(input('end_time')) + 86399;
        }

        $gids = getGroups(5);
        $where = [
            'duser.dep_id' => ['in',$gids],
            'user_status' => ['neq',0]
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname'] = ['like', "%$keyword%"];
        }

        $rongcuiPotentialCustomerModel = new RongcuiPotentialCustomerModel();
        $potential_customer = $rongcuiPotentialCustomerModel
            ->field('uid,count(uid) as ccount')
            ->where([
                'create_time' => ['between', [$time['starttime'], $time['endtime']]]
            ])
            ->group('uid')
            ->buildSql();

        $rongcuiBuyerModel = new RongcuiBuyerModel();
        $buyer = $rongcuiBuyerModel
            ->alias('buyer')
            ->join('__RONGCUI_BUYER_USER__ buser','buyer.id = buser.pid and level = 1')
            ->field('buser.uid,count(buser.uid) as ccount')
            ->where([
                'buyer.create_time' => ['between', [$time['starttime'], $time['endtime']]]
            ])
            ->group('buser.uid')
            ->buildSql();

        $userModel = new UserModel();
        $lists = $userModel
            ->alias('user')
            ->join('__DEPARTMENT_USER__ duser','user.id = duser.user_id')
            ->join('__ROLE_USER__ role_user','role_user.user_id = user.id','left')
            ->join('__ROLE_ITEM__ role_item','role_user.role_id = role_item.role_id and config_id = 1','left')
            ->join('__DEPARTMENT_CONFIG_ITEM__ item','item.id = role_item.item_id and item.pid = 1','left')
            ->join('__DEPARTMENT__ dep','duser.dep_id = dep.id')
            ->join('__ROLE__ role','role_user.role_id = role.id')
            ->join($potential_customer . ' potential_customer','user.id = potential_customer.uid','left')
            ->join($buyer . ' buyer','user.id = buyer.uid','left')
            ->where($where)
            ->field('user.user_nickname,ifnull(buyer.ccount,0) + ifnull(potential_customer.ccount,0) ccount,item.name iname,dep.name dname,role.name rname')
            ->order('ccount desc')
            ->select();

        $this->assign('lists',$lists);
        $this->assign('time', $time);
        return $this->fetch();
    }
}